package com.hcy.MovieData;

import com.entity.Movie;
import com.hcy.MainFrame;
import com.jch.Dao.UserDao;
import com.entity.User;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import java.util.regex.Pattern;

public class AddFrame extends JDialog {

    private JLabel jl_title;//标题

    private JPanel jp_north;//上部
    private JPanel jp_center;//中部
    private JPanel jp_south;//下部


    private JPanel jp_west;//左边
    private JPanel jp_east;//右边

    private JLabel jl_mName;//姓名标签
    private JTextField jtf_mName;//姓名文本域

    private JLabel jl_language;//语言标签
    private JTextField jtf_language;

    private JLabel jl_locality;//国家标签
    private JTextField jtf_locality;//国家文本域

    private JLabel jl_address;//地址标签
    private JTextField jtf_address;

    private JLabel jl_detail;//说明标签
    private JTextField jtf_detail;//

    private JLabel jl_duration;//时间标签
    private JTextField jtf_duration;

    private JLabel jl_type;//类型
    private JTextField jtf_type;

    private JLabel jl_grade;//评分
    private JTextField jtf_grade;

    private JButton jbt_submit;//提交

    private MovieFrame movieFrame;

    private int id;

    public AddFrame(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        this.movieFrame = (MovieFrame) owner;
        this.getContentPane().setBackground(Color.WHITE);
        setLayout(new BorderLayout());


        createContent();

        jp_north =  new JPanel();
        jl_title = new JLabel(title);
        jp_north.setBackground(Color.WHITE);
        setLabel(jl_title);
        jp_north.add(jl_title);
        add(jp_north, BorderLayout.NORTH);

        jp_center =  new JPanel();
        jp_center.setLayout(new BorderLayout());

        add(jp_center, BorderLayout.CENTER);

        jp_south =  new JPanel();
        jp_south.add(jbt_submit);
        jp_south.setBackground(Color.WHITE);
        add(jp_south, BorderLayout.SOUTH);


        jp_west = new JPanel();
        jp_west.setLayout(new GridLayout(8, 1, 20, 20));
        jp_west.setPreferredSize(new Dimension(100, 0));
        jp_west.setBackground(Color.WHITE);
        jp_center.add(jp_west, BorderLayout.WEST);
        jp_west.add(jl_mName);
        jp_west.add(jl_language);
        jp_west.add(jl_locality);
        jp_west.add(jl_address);
        jp_west.add(jl_detail);
        jp_west.add(jl_duration);
        jp_west.add(jl_type);
        jp_west.add(jl_grade);

        jp_east = new JPanel();
        jp_east.setLayout(new GridLayout(8, 1, 20, 20));
        jp_east.setBackground(Color.WHITE);
        jp_center.add(jp_east, BorderLayout.CENTER);

        jp_east.add(jtf_mName);
        jp_east.add(jtf_language);
        jp_east.add(jtf_locality);
        jp_east.add(jtf_address);
        jp_east.add(jtf_detail);
        jp_east.add(jtf_duration);
        jp_east.add(jtf_type);
        jp_east.add(jtf_grade);

        if (this.movieFrame.getPnMovie()!= null) {
            Movie movie = this.movieFrame.getPnMovie();
            id = movie.getMid();
            jtf_mName.setText(movie.getmName());
            jtf_language.setText(movie.getMlanguage());
            jtf_locality.setText(movie.getMlocality());
            jtf_address.setText(movie.getmPicaddress());
            jtf_detail.setText(movie.getmDetail());
            jtf_duration.setText(movie.getmDuration()+"");
            jtf_type.setText(movie.getmType());
            jtf_grade.setText(movie.getmGrade()+"");
        }

        //1.先把窗口显示出来
        //标题
        setTitle("增删改查");
        //尺寸
        setSize(600,600);
        //居中
        setLocationRelativeTo(null);
        //设置关闭模式。直接结束整个程序
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //设置可见
        setVisible(true);

    }

    public void createContent() {
        jl_mName = new JLabel("电影名称:");
        setLabel(jl_mName);
        jtf_mName = new JTextField();

        jl_language = new JLabel("语言：");
        setLabel(jl_language);
        jtf_language = new JTextField();

        jl_locality = new JLabel("国家：");
        setLabel(jl_locality);
        jtf_locality = new JTextField();


        jl_address = new JLabel("地址:");
        setLabel(jl_address);
        jtf_address = new JTextField();

        jl_detail = new JLabel("说明:");
        setLabel(jl_detail);
        jtf_detail = new JTextField();


        jl_duration = new JLabel("时间(数字):");
        setLabel(jl_duration);
        jtf_duration = new JTextField();
        //限制输入的只能为数字
        jtf_duration.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar >= '0' && keyChar<= '9'){
                    return;

                }
                Toolkit.getDefaultToolkit().beep();

                e.consume();
            }
        });

        jl_type = new JLabel("类型:");
        setLabel(jl_type);
        jtf_type = new JTextField();


        jl_grade = new JLabel("评分(数字):");
        setLabel(jl_grade);
        jtf_grade = new JTextField();
        //限制输入的只能为数字
        jtf_grade.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar >= '0' && keyChar<= '9'){
                    return;

                }
                Toolkit.getDefaultToolkit().beep();

                e.consume();
            }
        });
        jbt_submit = new JButton("提交");
        jbt_submit.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Movie movie = getMovie();
                System.out.println(movie);
                int i = 0;
                if (movieFrame.getPnMovie() == null) {
                    //当前用户没有值，增加
                    i = movieFrame.getMovieDao().insertMovie(movie);
                } else {
                    i = movieFrame.getMovieDao().updateMovie(movie);
                }


                JOptionPane.showMessageDialog(AddFrame.this, i>0?"提交成功":"提交失败");
                //关闭自己
                dispose();
            }
        });

    }

    public Movie getMovie() {
        String name = jtf_mName.getText();
        String language = jtf_language.getText();
        String locality = jtf_locality.getText();
        String address = jtf_address.getText();
        String detail = jtf_detail.getText();
        int duration = Integer.parseInt(jtf_duration.getText());
        String type = jtf_type.getText();
        double grade = Double.parseDouble(jtf_grade.getText());
        return new Movie(id,name,language,locality,address,detail,duration,type,grade);
    }

    public void setLabel(JLabel jl) {
        //水平居中
        jl.setHorizontalAlignment(SwingConstants.RIGHT);
        //垂直居中
        jl.setVerticalAlignment(SwingConstants.CENTER);
        //设置字体
        jl.setFont(new Font("微软雅黑", Font.PLAIN, 18));

    }

    public static void main(String[] args) {
         //new AddFrame(null, "增加数据", true);
    }

}